﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Diagnostics;

namespace GemBlaster.UI
{
    public class GameScreen : Control
    {
		public GameScreen()
		{
            Stream s = this.GetType().Assembly.GetManifestResourceStream("GemBlaster.UI.GameScreen.xaml");
			_root = this.InitializeFromXaml(new StreamReader(s).ReadToEnd());

            _mat = _root.FindName("Mat") as System.Windows.Shapes.Rectangle;
            _button1Text = _root.FindName("ButtonOneText") as TextBlock;
            _button2Text = _root.FindName("ButtonTwoText") as TextBlock;
            _button3Text = _root.FindName("ButtonThreeText") as TextBlock;
            _button4Text = _root.FindName("ButtonFourText") as TextBlock;

            _Btn1Hilight = _root.FindName("ButtonOneHilight") as System.Windows.Shapes.Path;
            _Btn2Hilight = _root.FindName("ButtonTwoHilight") as System.Windows.Shapes.Path;
            _Btn3Hilight = _root.FindName("ButtonThreeHilight") as System.Windows.Shapes.Path;
            _Btn4Hilight = _root.FindName("ButtonFourHilight") as System.Windows.Shapes.Path;
            _currentScore = _root.FindName("CurrentScoreText") as System.Windows.Documents.Run;
            _highScore = _root.FindName("HighScoreText") as System.Windows.Documents.Run;

            SetHoverEvents(_Btn1Hilight);
            SetHoverEvents(_Btn2Hilight);
            SetHoverEvents(_Btn3Hilight);
            SetHoverEvents(_Btn4Hilight);

            _Btn1Hilight.MouseLeftButtonDown += new MouseEventHandler(_Btn1Hilight_MouseLeftButtonDown);
            _Btn2Hilight.MouseLeftButtonDown += new MouseEventHandler(_Btn2Hilight_MouseLeftButtonDown);
            _Btn3Hilight.MouseLeftButtonDown += new MouseEventHandler(_Btn3Hilight_MouseLeftButtonDown);
            _Btn4Hilight.MouseLeftButtonDown += new MouseEventHandler(_Btn4Hilight_MouseLeftButtonDown);
        }

        void _Btn1Hilight_MouseLeftButtonDown(object sender, MouseEventArgs e)
        {
            if (Button1Click != null)
                Button1Click(this, new EventArgs());
        }

        void _Btn2Hilight_MouseLeftButtonDown(object sender, MouseEventArgs e)
        {
            if (Button2Click != null)
                Button2Click(this, new EventArgs());
        }

        void _Btn3Hilight_MouseLeftButtonDown(object sender, MouseEventArgs e)
        {
            if (Button3Click != null)
                Button3Click(this, new EventArgs());
        }

        void _Btn4Hilight_MouseLeftButtonDown(object sender, MouseEventArgs e)
        {
            if (Button4Click != null)
                Button4Click(this, new EventArgs());
        }

        void SetHoverEvents(System.Windows.Shapes.Path path)
        {
            Storyboard Hover = _root.FindName(path.Name + "Hover") as Storyboard;
            path.Opacity = 0;
            path.MouseEnter += delegate(object sender, MouseEventArgs e) 
            {
                Hover.Begin(); 
            };
            path.MouseLeave += delegate(object sender, EventArgs e) 
            { 
                Hover.Stop();
            };
        }

        FrameworkElement _root;
        System.Windows.Shapes.Rectangle _mat;
        TextBlock _button1Text;
        TextBlock _button2Text;
        TextBlock _button3Text;
        TextBlock _button4Text;
        System.Windows.Shapes.Path _Btn1Hilight;
        System.Windows.Shapes.Path _Btn2Hilight;
        System.Windows.Shapes.Path _Btn3Hilight;
        System.Windows.Shapes.Path _Btn4Hilight;
        System.Windows.Documents.Run _currentScore;
        System.Windows.Documents.Run _highScore;

        public System.Windows.Shapes.Rectangle Mat
        {
            get
            {
                return _mat;
            }
        }

        public event EventHandler Button1Click;
        public event EventHandler Button2Click;
        public event EventHandler Button3Click;
        public event EventHandler Button4Click;

        public string Button1Text
        {
            get
            {
                return _button1Text.Text;
            }
            set
            {
                _button1Text.Text = value;
            }
        }

        public string Button2Text
        {
            get
            {
                return _button2Text.Text;
            }
            set
            {
                _button2Text.Text = value;
            }
        }

        public string Button3Text
        {
            get
            {
                return _button3Text.Text;
            }
            set
            {
                _button3Text.Text = value;
            }
        }

        public string Button4Text
        {
            get
            {
                return _button4Text.Text;
            }
            set
            {
                _button4Text.Text = value;
            }
        }

        long _high = 0;
        long _current = 0;

        public long HighScore
        {
            get
            {
                return _high;
            }
            set
            {
                _high = value;
                _highScore.Text = value.ToString();
            }
        }

        public long CurrentScore
        {
            get
            {
                return _current;
            }
            set
            {
                _current = value;
                _currentScore.Text = value.ToString();
            }
        }
    }
}